package com.lrr.community.controller;

import com.lrr.community.dto.AccessTokenDto;
import com.lrr.community.dto.GithubUser;
import com.lrr.community.mapper.UserMapper;
import com.lrr.community.model.User;
import com.lrr.community.provider.GithubProvider;
import com.lrr.community.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.UUID;

@Controller
public class AuthController {

    // 注册Component的类，在当前Controller使用
    @Autowired
    private GithubProvider githubProvider;

    // 注册数据库操作类，在当前Controller使用
    @Autowired
    private UserMapper userMapper;
    @Value("${github.client.id}")
    private String clientId;
    @Value("${github.client.secret}")
    private String clientSecret;

    @Autowired
    private UserService userService;

    @Value("${github.client.redirect_uri}")
    private String clientRedirect_uri;
    @GetMapping("/callback")
    public String callback(@RequestParam(name = "code") String code,
                           @RequestParam(name = "state") String state,
                           HttpServletRequest request,
                           HttpServletResponse response){
        AccessTokenDto accessTokenDto = new AccessTokenDto();
        accessTokenDto.setCode(code);
        accessTokenDto.setRedirect_uri(clientRedirect_uri);
        accessTokenDto.setState(state);
        accessTokenDto.setClient_id(clientId);
        accessTokenDto.setClient_secret(clientSecret);
        String accessToken = githubProvider.getAccessToken(accessTokenDto);

        GithubUser githubUser = githubProvider.getUser(accessToken);
        System.out.println(githubUser.getName()+" "+githubUser.getBio()+" "+githubUser.getId());
        if(githubUser!=null && githubUser.getName()!=null){
            // 登录成功
            request.getSession().setAttribute("user",githubUser);
            User user = new User();
            String token = UUID.randomUUID().toString();
            user.setToken(token);
            user.setName(githubUser.getName());
            user.setAccount_id(String.valueOf(githubUser.getId()));
            user.setGmt_create(System.currentTimeMillis());
            user.setGmt_modified(user.getGmt_create());
            user.setAvatar_url(githubUser.getAvatar_url());
            user.setBio(githubUser.getBio());
            System.out.println(user);

            userService.createOrUpdate(user);

            response.addCookie(new Cookie("token",token));
            return "redirect:/"; // 跳转到首页
        }else{
            // 登录失败
            return "redirect:/"; // 跳转到首页
        }
    }
    @GetMapping("/logout")
    public String logout(HttpServletRequest request,HttpServletResponse response){
        request.getSession().removeAttribute("user");
        response.addCookie(new Cookie("token",null));
        return "redirect:/";
    }
}
